﻿/*
 * @Author: Song Xinxin
 * @Date: 2022-11-02 15:03:15
 * @LastEditors: Song Xinxin
 * @LastEditTime: 2022-11-07 12:40:22
 * @FilePath: \WYW_0419_Oasis\JavaScripts\UI\UILeaderboard.ts
 * @Description: 
 * 
 * Copyright (c) 2022 by error: git config user.name && git config user.email & please set dead value or install git, All Rights Reserved. 
 */
import { DataCenterC, ModuleManager, UI } from "odin";
import { Data_Game } from "../Modules/Game/Data_Game";
import { ModuleC_Game } from "../Modules/Game/Module_Game";
import { Data_PlayerAttr } from "../Modules/PlayerAttr/Data_PlayerAttr";
import { ModuleC_Sound } from "../Modules/Sound/Module_Sound";
import { UIStart } from "./UIStart";
import { UI_LeaderboardUI } from "./UITemplate";

export class UILeaderboard extends UI_LeaderboardUI
{

    //名次
    TextRank:MWGameUI.MWUITextblock[] = [];
    //玩家昵称
    TextName:MWGameUI.MWUITextblock[] = [];
    //玩家击杀数
    TextKill:MWGameUI.MWUITextblock[] = [];
    //玩家死亡数
    TextDeath:MWGameUI.MWUITextblock[] = [];
    //玩家收集数
    TextCollection:MWGameUI.MWUITextblock[] = [];
    //显示自己
    ImageSelf:MWGameUI.MWUIImage[] = [];

    protected onStart(): void 
    {
        this.initButton();
        this.initRank();
        this.initSelf();    
    }

    protected onShow(): void 
    {
        ModuleManager.instance.getModule(ModuleC_Sound).playOverBGM();

        this.setRank();
        this.showRank();
        this.showSelf();
    }

    public initButton():void
    {
        this.button_Close.onClicked().add(()=>
        {
            ModuleManager.instance.getModule(ModuleC_Sound).ButtonSound();
            UI.instance.hidePanel(UILeaderboard);
            UI.instance.showPanel(UIStart);
        })
    }

    /**
     * 初始化排行榜文本内容
     */
    public initRank():void
    {
        this.TextRank[0] = this.text_rank_1;
        this.TextRank[1] = this.text_rank_2;
        this.TextRank[2] = this.text_rank_3;
        this.TextRank[3] = this.text_rank_4;
        this.TextRank[4] = this.text_rank_5;
        this.TextRank[5] = this.text_rank_6;

        this.TextName[0] = this.text_Name_1;
        this.TextName[1] = this.text_Name_2;
        this.TextName[2] = this.text_Name_3;
        this.TextName[3] = this.text_Name_4;
        this.TextName[4] = this.text_Name_5;
        this.TextName[5] = this.text_Name_6;

        this.TextKill[0] = this.text_Kill_1;
        this.TextKill[1] = this.text_Kill_2;
        this.TextKill[2] = this.text_Kill_3;
        this.TextKill[3] = this.text_Kill_4;
        this.TextKill[4] = this.text_Kill_5;
        this.TextKill[5] = this.text_Kill_6;

        this.TextDeath[0] = this.text_Death_1;
        this.TextDeath[1] = this.text_Death_2;
        this.TextDeath[2] = this.text_Death_3;
        this.TextDeath[3] = this.text_Death_4;
        this.TextDeath[4] = this.text_Death_5;
        this.TextDeath[5] = this.text_Death_6;

        this.TextCollection[0] = this.text_cllection_1;
        this.TextCollection[1] = this.text_cllection_2;
        this.TextCollection[2] = this.text_cllection_3;
        this.TextCollection[3] = this.text_cllection_4;
        this.TextCollection[4] = this.text_cllection_5;
        this.TextCollection[5] = this.text_cllection_6;

        for(let index of this.TextRank)
        {
            index.setText("Default");
        }

        for(let index of this.TextName)
        {
            index.setText("Default");
        }

        for(let index of this.TextKill)
        {
            index.setText("Default");
        }

        for(let index of this.TextDeath)
        {
            index.setText("Default");
        }

        for(let index of this.TextCollection)
        {
            index.setText("Default");
        }
        this.hideText();
        
    }

    /**
     * 初始化显示自己
     */
    public initSelf():void
    {
        this.ImageSelf[0] = this.image_Self_1;
        this.ImageSelf[1] = this.image_Self_2;
        this.ImageSelf[2] = this.image_Self_3;
        this.ImageSelf[3] = this.image_Self_4;
        this.ImageSelf[4] = this.image_Self_5;
        this.ImageSelf[5] = this.image_Self_6;
        for(let index of this.ImageSelf)
        {
            index.setVisibility(MWGameUI.ESlateVisibility.Collapsed);
        }
    }

    /**
     * 显示自己
     */
    public showSelf():void
    {
        let PlayerNum = DataCenterC.instance.getModuleData(Data_Game).getNameRank.length;
        let IDRank = ModuleManager.instance.getModule(ModuleC_Game).getIDRank;
        let ID = ModuleManager.instance.getModule(ModuleC_Game).getID;
        for(let index = 0;index < PlayerNum;index++)
        {
           if(ID == IDRank[index])
           {
                this.ImageSelf[index].setVisibility(MWGameUI.ESlateVisibility.SelfHitTestInvisible);
           }
        }
    }

    /**
     * 设置排行榜文本内容
     */
    public setRank():void
    {
        let Rank = DataCenterC.instance.getModuleData(Data_Game);
        let PlayerNum = Rank.getNameRank.length;
        for(let index = 0;index < PlayerNum;index++)
        {
            this.TextRank[index].setText((index+1).toString());
            this.TextName[index].setText(Rank.getNameRank[index]);
            this.TextKill[index].setText((Rank.getKillRank[index]).toString());
            this.TextDeath[index].setText((Rank.getDeathRank[index]).toString());
            this.TextCollection[index].setText((Rank.getCountRank[index]).toString());
        }
    }

    /**
     * 隐藏排行榜文本内容
     */
    public hideText():void
    {
        for(let index = 0;index < 6;index++)
        {
            this.TextRank[index].setVisibility(MWGameUI.ESlateVisibility.Collapsed);
            this.TextName[index].setVisibility(MWGameUI.ESlateVisibility.Collapsed);
            this.TextKill[index].setVisibility(MWGameUI.ESlateVisibility.Collapsed);
            this.TextDeath[index].setVisibility(MWGameUI.ESlateVisibility.Collapsed);
            this.TextCollection[index].setVisibility(MWGameUI.ESlateVisibility.Collapsed);
        }
    }

    /**
     * 展示排行榜文本内容
     */
    public showRank():void
    {
        for(let index = 0;index < 6;index++)
        {
            if(this.TextRank[index].getText() != "Default")
            {
                this.TextRank[index].setVisibility(MWGameUI.ESlateVisibility.SelfHitTestInvisible);
            }

            if(this.TextName[index].getText() != "Default")
            {
                this.TextName[index].setVisibility(MWGameUI.ESlateVisibility.SelfHitTestInvisible);
            }

            if(this.TextKill[index].getText() != "Default")
            {
                this.TextKill[index].setVisibility(MWGameUI.ESlateVisibility.SelfHitTestInvisible);
            }

            if(this.TextDeath[index].getText() != "Default")
            {
                this.TextDeath[index].setVisibility(MWGameUI.ESlateVisibility.SelfHitTestInvisible);
            }
            if(this.TextCollection[index].getText() != "Default")
            {
                this.TextCollection[index].setVisibility(MWGameUI.ESlateVisibility.SelfHitTestInvisible);
            }
        }
    }
}